<?php





namespace app\admin\controller;





use app\admin\model\ShopModel;

use app\common\Constants\ErrorCode;

use app\common\traits\ActionRecordTrait;

use think\response\Json;



class Shop extends ApiCommon

{



    use ActionRecordTrait;

    /**

     * 用于判断权限

     * @permission 无限制

     * @allow 登录用户可访问

     * @author Mr.Li

     * @create 2021-05-26 16:20

     */

    public function initialize()

    {

        $action = [

            'permission' => ['exportcsv','import'],

            'allow' => [

                'synckingdeestock'

            ]

        ];

        event('CheckAuth', $action);

        $request = $this->request;

        $a       = strtolower($request->action());

        if (!in_array($a, $action['permission'])) {

            parent::initialize();

        }

    }

    /**

     * 创建店铺资源

     * @author ran

     * @date 2021-09-16 14:12

     * mailbox 466180170@qq.com

     */

    public function createAdminShopResource():Json

    {

        $request = $this->request->param();

        $model = new ShopModel();

        $res          = $model->createShopResource($request);

        if (!$res) {

            return  $this->failed(ErrorCode::SYSTEM_INVALID,$model->getError());

        }

        return  $this->success([],'创建成功');

    }

    /**

     * 更新

     * @author ran

     * @date 2021-09-25 15:47

     * mailbox 466180170@qq.com

     */

    public function updateAdminShopResource(){

        $request = $this->request->param();

        $model = new ShopModel();

        $res          = $model->updateShopResource($request);

        if (!$res) {

            return  $this->failed(ErrorCode::SYSTEM_INVALID,$model->getError());

        }

        return  $this->success([],'更新成功');

    }

    /**

     * 更新

     * @author ran

     * @date 2021-09-25 15:47

     * mailbox 466180170@qq.com

     */

    public function updateAdminShopPersonResource(){

        $request = $this->request->param();

        $model = new ShopModel();

        $res          = $model->updateShopPersonResource($request);

        if (!$res) {

            return  $this->failed(ErrorCode::SYSTEM_INVALID,$model->getError());

        }

        return  $this->success([],'更新成功');

    }
	
	/**
	
	 * @return Json
	
	 * @author ran
	
	 * @date 2021-09-22 17:09
	
	 * mailbox 466180170@qq.com
	
	 */
	
	 public function readAdminMarketList():Json
	
	 {
	 
	     $request = $this->request->param();
	 
	     $shopIds = $this->getCommonShopIdsCollectByUserType();
	 
	     $model = new MarketModel();
	 
	     if (empty($request['page']) || empty($request['limit'])) return $this->failed(ErrorCode::SYSTEM_INVALID);
	 
	     $res          = $model->getMarketList($shopIds,$request);
	 
	     return $this->success($res,'获取资源成功');
	 
	 }
	
	
    /**

     * @return Json

     * @author ran

     * @date 2021-09-22 17:09

     * mailbox 466180170@qq.com

     */

    public function readAdminShopList():Json

    {

        $request = $this->request->param();

        $shopIds = $this->getCommonShopIdsCollectByUserType();

        $model = new ShopModel();

        if (empty($request['page']) || empty($request['limit'])) return $this->failed(ErrorCode::SYSTEM_INVALID);

        $res          = $model->getShopAdminListResource($shopIds,(int)$request['page'],(int)$request['limit'], $request);

        return $this->success($res,'获取资源成功',$res['dataCount']);

    }

    /**

     * 获取店铺详情

     * @return Json

     * @author ran

     * @date 2021-09-22 17:09

     * mailbox 466180170@qq.com

     */

    public function readAdminShopInfo():Json

    {

        $shopId = $this->request->param('shopId');

        $shopId =!empty($this->userInfo['shop_id'])?$this->userInfo['shop_id']:$shopId;

        if (empty($shopId)) return $this->failed(ErrorCode::SYSTEM_INVALID);

        $model = new ShopModel();

        $res          = $model->getShopAdminInfoResource((int)$shopId);

        return $this->success($res,'获取成功');

    }

    /**

     * 获取登录人详情

     * @return Json

     * @author ran

     * @date 2021-09-22 17:09

     * mailbox 466180170@qq.com

     */

    public function readAdminUserInfo():Json

    {
        $user_id = $this->userInfo['id'];

        $model = new ShopModel();

        $res   = $model->getUserInfoResource($user_id);

        return $this->success($res,'获取成功');

    }




    /**

     * 获取店铺关联市场

     * @return Json

     * @author ran

     * @date 2021-09-22 17:09

     * mailbox 466180170@qq.com

     */

    public function readAdminShopRelationMarketByShopId():Json

    {

        $shopId = $this->request->param('shopId');

        $shopId =!empty($this->userInfo['shop_id'])?$this->userInfo['shop_id']:$shopId;

        if (empty($shopId)) return $this->failed(ErrorCode::SYSTEM_INVALID);

        $model = new ShopModel();

        $res          = $model->readAdminShopRelationMarketByShopId((int)$shopId);

        return $this->success($res,'获取成功');

    }



    /**

     * 导出店铺资源信息

     * @return Json

     * @author ran

     * @date 2021-12-01 12:53

     * mailbox 466180170@qq.com

     */

    public function exportAdminShopList()

    {

        $request = $this->request->param();

        $shopIds = $this->getCommonShopIdsCollectByUserType();

        $requestId = isset($request['id']) && $request['id'] ? $request['id'] : [];

        if($requestId){

            $ids = is_array($requestId) ? $requestId : explode(',', $requestId);

        }else{

            $ids = [];

        }

        $model = new ShopModel();

        $res          = $model->exportAdminShopListResoure($shopIds,$request,$ids);

        return $this->success($res,'获取成功');

    }

    /**

     * 手动更新店铺信息

     * @return Json

     * @author ran

     * @date 2021-09-22 17:09

     * mailbox 466180170@qq.com

     */

    public function shopHandel():Json

    {

        $model = new ShopModel();

        $res          = $model->shopHandel();

        return $this->success($res,'获取成功');

    }



}